<?php
	class CompDormitorylease extends AppModel
	{
		var $name = "CompDormitorylease";
		var $primarykey = 'id';
		
		//计算传入日期和今天之间的天数
		function calcDaysBetweenTwoDate($date)
		{
			$now_date = date("Y-m-d");   //今天的日期
	        $deadline = date("Y-m-d",strtotime($date));   
	        $d1=strtotime($now_date);   
	        $d2=strtotime($deadline);   
	        $days=round(($d2-$d1)/3600/24); 
	        return $days;
		}
		
		//当租赁时间小于62天的时候显示提醒信息,在/app/vendor/中的alert.php调用
		function alertmessage()
		{
			$where = "CompDormitorylease.is_deleted = '0'";
			$deadlines = $this->find("all",array('conditions'=>array("CompDormitorylease.is_deleted='0'"),
													'fields'=>array("CompDormitorylease.id,CompDormitorylease.deadline,CompDormitorylease.hasBeenAlerted")			
												));
			foreach($deadlines as $deadline)
			{
				$dormitory_isNeedToAlert = false;
				$dormitorylease_deadline = $deadline["CompDormitorylease"]["deadline"];
				$dormitorylease_id = $deadline["CompDormitorylease"]["id"];
				$hasBeenAlerted = $deadline["CompDormitorylease"]["hasBeenAlerted"];
				$days = $this->calcDaysBetweenTwoDate($dormitorylease_deadline);
				if($days >= 0 && $days <= 62 && $hasBeenAlerted == '0')
				{
					$dormitory_isNeedToAlert = true;
					$this->id = $dormitorylease_id;
					$this->saveField('hasBeenAlerted', '1');
					App::import('Helper', 'Accp');
					$accp = new AccpHelper();
					$data=array("guid"=>$dormitorylease_id,"event"=>"dormitory_twomonths_left","usertype"=>"1","dormitory_isNeedToAlert"=>$dormitory_isNeedToAlert);
					$isflag=$accp->accpalert($data);
				}
			}
		}
		
	}
?>